package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.mapper.TypeMapper;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.Type;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.BrandMapper;
import com.bwie.pojo.Brand;
import com.bwie.service.BrandService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService{
    @Autowired
    BrandMapper brandMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    TypeMapper typeMapper;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public R brandList(PageInfoVo pageInfoVo) {
        Page<Brand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().eq(Brand::getBrandName,pageInfoVo.getKeyWord());
        }
        wrapper.lambda().orderByDesc(Brand::getCreateTime);
        Page<Brand> brandPage = brandMapper.selectPage(page, wrapper);
        return R.OK(brandPage);
    }

    @Override
    public R updateStatus(Brand brand) {
        Brand brand1 = brandMapper.selectById(brand.getBrandId());
        if(brand1==null){
            return R.ERROR(500,"品牌不存在");
        }

        String token = request.getHeader("token");
        if(token==null){
            return R.ERROR(500,"请先登录");
        }
        String userName = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody().get("userName", String.class);

        brand.setCreateBy(userName);
        brand.setBrandName(brand.getBrandName());
        brand.setCreateTime(new Date());
        brand.setBrandTop(brand.getBrandTop());
        brand.setBrandUse(brand.getBrandUse());
        brand.setBrandStatus(brand.getBrandStatus());
        brandMapper.updateById(brand);
        return R.OK();
    }

    @Override
    public R addBrand(Brand brand) {
        String token = request.getHeader("token");
        if(token==null){
            return R.ERROR(500,"请先登录");
        }
        String userName = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody().get("userName", String.class);

        brand.setCreateBy(userName);
        brand.setBrandName(brand.getBrandName());
        brand.setCreateTime(new Date());
        brand.setBrandTop(brand.getBrandTop());
        brand.setBrandUse(brand.getBrandUse());
        brand.setBrandStatus(brand.getBrandStatus());
        brandMapper.insert(brand);
        return R.OK();
    }

    @Override
    public R typeTreeList(PageInfoVo pageInfoVo) {
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        if(pageInfoVo.getTypeId()!=null){
            wrapper.eq(Type::getTypeStatus,pageInfoVo.getTypeId());
        }
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.like(Type::getTypeName,pageInfoVo.getKeyWord());
        }

        List<Type> typeList = typeMapper.selectList(wrapper);
        List<Tree<String>> treeList = TreeUtil.build(typeList, "0", (tree, node) -> {
            node.setId(tree.getTypeId().toString());
            node.setParentId(tree.getTypePid().toString());
            node.setName(tree.getTypeName());
            node.putExtra("typeImage", tree.getTypeImage());
            node.putExtra("typeTop", tree.getTypeTop());
            node.putExtra("typeStatus", tree.getTypeStatus());
        });
        return R.OK(treeList);
    }

    @Override
    public R addType(Type type) {
        Type type1 = typeMapper.selectOne(new LambdaQueryWrapper<Type>().eq(Type::getTypeName, type.getTypeName()));
        if(type1 != null){
            return R.ERROR(500,"名称已存在，请重新输入");
        }
        typeMapper.insert(type);
        return R.OK();
    }

    @Override
    public R uploadTypeImage(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ERROR(500,"上传失败");
    }

    @Override
    public R getbrandList() {
        List<Brand> brandList = brandMapper.selectList(null);
        return R.OK(brandList);
    }

    @Override
    public R gettypeList() {
        List<Type> typeList = typeMapper.selectList(null);
        return R.OK(typeList);
    }


}
